package com.lin.filegraph.utils.name;

import com.lin.filegraph.utils.threshold.Threshold;
import com.lin.filegraph.utils.compdg.ComponentGraph;
import com.lin.filegraph.utils.compdg.ComponentNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 15:43
 */

public class ComRename {
    public static void executeUnionWithDir(ComponentGraph graph) {
        renameCDG(graph);
//		System.out.println("改名后——————");
//		graph.displayGraph();
        unionWithDir(graph);
    }

    public static void unionWithDir(ComponentGraph graph) {
        boolean isUnion = false;
        for (int i = 0; i < graph.getAllComponents().size(); i++) {
            ComponentNode cn1 = graph.getAllComponents().get(i);
            double simMax = 0;
            ComponentNode tempCn = null;
            for (int j = 0; j < graph.getAllComponents().size() && j != i; j++) {
                ComponentNode cn2 = graph.getAllComponents().get(j);
                double simpath = DirSimilarity.getSimLevelOfPath(cn1.getStrComponentName(), cn2.getStrComponentName());
                if (simpath > simMax) {
                    simMax = simpath;
                    tempCn = cn2;
                }
                if (simpath != 0 && simpath == simMax && tempCn.getAllFiles().size() > cn2.getAllFiles().size()) {
                    tempCn = cn2;
                }
            }

            if (cn1.getAllFiles().size() < 2) {
                if (simMax >= Threshold.reNameSep) {
                    String str = DirSimilarity.getSimByPath(cn1.getStrComponentName(), tempCn.getStrComponentName());
                    graph.mergeNodes(cn1, tempCn);
                    cn1.setStrComponentName(str);
                    isUnion = true;
                    break;
                }
            } else {
                // if (simMax >= Threshold.reNameSep) {
                // String str = getSimByPath(cn1.getStrComponentName(),
                // tempCn.getStrComponentName());
                // ComponentEdge cEdge1 = graph.getCompEdge(cn1, tempCn);
                // ComponentEdge cEdge2 = graph.getCompEdge(tempCn, cn1);
                // if(cEdge1!=null||cEdge2!=null){
                // graph.mergeNodes(cn1, tempCn);
                // cn1.setStrComponentName(str);
                // isUnion = true;
                // break;
                // }
                // }
            }
        }

        if (isUnion) {
            unionWithDir(graph);
        }
    }

    public static void renameCDGOrMudole(ComponentGraph graph) {
        // 编号
        graph.setNumBer();

        for (ComponentNode cn : graph.getAllComponents()) {
            String dir = getComNameByDir(cn);
            cn.getAllDirs().clear();
            cn.reComName(dir);
            cn.addDir(cn.getStrComponentName());
        }
    }

    public static void reDir(ComponentGraph graph) {
        // 编号
//		graph.setNumBer();

        for (ComponentNode cn : graph.getAllComponents()) {
            String dir = getComNameByDir(cn);
            cn.reComName(dir);
        }
    }


    public static void renameCDG(ComponentGraph graph) {
        graph.setNumBer();
        for (ComponentNode cn : graph.getAllComponents()) {
            String dir = getComNameByDir(cn);
            cn.reComName(dir);
        }
    }

    public static void renameNode(ComponentGraph graph,ComponentNode cn2){
        cn2.setID(graph.addLastNum());
        String dir = getComNameByDir(cn2);
        cn2.reComName(dir);
    }

//	public static String getComNameByDir2(ComponentNode cn) {
//		String maxstr = "";
//		// 所有文件最多的路径
//		// Map<String, Integer> subMap = new HashMap<>();
//		List<String> subMap = new ArrayList<>();
//		if (cn.getAllFiles().size() > 1) {
//			for (int i = 0; i < cn.getAllFiles().size(); i++) {
//				String str1 = cn.getAllFiles().get(i);
//				int num = 1;
//				for (int j = 0; j < cn.getAllFiles().size(); j++) {
//					String str2 = cn.getAllFiles().get(j);
//					if (!str1.equals(str2)) {
//						if (DirSimilarity.getSimLevelOfPath(str1, str2) != 0) {
//							num++;
//						}
//					}
//				}
//				if (num >= cn.getAllFiles().size() * Threshold.reNameSep) {
//					subMap.add(str1);
//				}
//			}
//			if (!subMap.isEmpty()) {
//				maxstr = subMap.get(0);
//				for (int k = 1; k < subMap.size(); k++) {
//					maxstr = DirSimilarity.getSimByPath(maxstr, subMap.get(k));
//				}
//			}
//		} else {
//			String fileStr = cn.getAllFiles().get(0);
//			String rePath = Threshold.ROOT;
//			if (fileStr.contains("/"))
//				rePath = fileStr.substring(0, fileStr.lastIndexOf("/"));
//			maxstr = rePath;
//		}
//		return maxstr;
//	}

    public static String getComNameByDir(ComponentNode cn) {
        String maxstr = "";
        int num = 0;
        Map<String, Integer> sub = new HashMap<>();
        // 所有文件最多的路径
        if (cn.getAllFiles().size() > 1) {
            for (int i = 0; i < cn.getAllFiles().size(); i++) {
                Map<String, Integer> subMap = new HashMap<>();
                String str1 = cn.getAllFiles().get(i);
                String simStr = str1;
                int simNum = 1;
                for (int j = 0; j < cn.getAllFiles().size(); j++) {
                    String str2 = cn.getAllFiles().get(j);
                    if (!str1.equals(str2)) {
                        String str = DirSimilarity.getSimByPath(str1, str2);
                        if (str != null && !str.equals("")) {
                            int sum = subMap.get(str) != null ? subMap.get(str) + 1 : 2;
                            subMap.put(str, sum);
                            String s = DirSimilarity.getSimByPath(simStr, str);
                            if (s != "") {
                                simStr = s;
                                simNum++;
                            }
                            if (sum > num)
                                num = sum;
                        }
                    }
                }
//				System.out.println(cn.getAllFiles().size() * Threshold.reNameSim);
//				System.out.println("num-"+num);
//				System.out.println("simNum-"+simNum);
//				System.out.println("AA-"+simStr);
                if (num <= cn.getAllFiles().size() * Threshold.reNameSim) {
                    if (simNum > cn.getAllFiles().size() * Threshold.reNameSim)
                        maxstr = simStr;
                } else {
                    for (Map.Entry<String, Integer> e : subMap.entrySet()) {
                        if (e.getValue() > cn.getAllFiles().size() * Threshold.reNameSim) {
                            if (e.getKey().length() > maxstr.length() || (e.getKey().length() == maxstr.length() && e.getValue() > num)) {
                                maxstr = e.getKey();
                                num = e.getValue();
                            }
                        }
                    }
                }
//				System.out.println("maxStr-"+maxstr);
                if (!maxstr.equals(""))
                    sub.put(maxstr, sub.get(maxstr) != null ? sub.get(maxstr) + 1 : 1);
                maxstr = "";
                num = 0;
            }
            for (Map.Entry<String, Integer> se : sub.entrySet()) {
                if (se.getValue() > num || (se.getValue() == num && se.getKey().length() > maxstr.length())) {
                    maxstr = se.getKey();
                    num = se.getValue();
                }
            }
        } else {
            String fileStr = cn.getAllFiles().get(0);
            String rePath = Threshold.ROOT;
            if (fileStr.contains("/"))
                rePath = fileStr.substring(0, fileStr.lastIndexOf("/"));
            maxstr = rePath;
        }
        return maxstr;
    }
}

